<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>关键词管理管理</title>
	<meta name="decorator" content="default"/>
</head>
<body>
<div class="main-content">
    <tags:breadcrumb menuId="f6dbf12b87a743838fcb7ea18a61bc59" />
    <div class="page-content">
        <form:form id="searchForm" modelAttribute="autoResponse" action="${ctx}/wechat/autoResponse/" method="post" class="form-horizontal">
            <input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
            <input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
        </form:form>
        <table id="contentTable" class="table table-striped table-bordered table-hover">
            <thead>
                <tr>
                    <th>公众号</th>
                    <th>关键字</th>
                    <th>模板名称</th>
                    <th>消息类型</th>
                    <th>添加时间</th>
                    <shiro:hasPermission name="wechat:autoResponse:view">
                        <th>操作</th>
                    </shiro:hasPermission>
                </tr>
            </thead>
            <tbody>
            <c:forEach items="${list}" var="autoResponse">
                <tr>
                    <td accountid="${autoResponse.accountid}">${autoResponse.accountname}</td>
                    <td><a href="${ctx}/wechat/autoResponse/form?id=${autoResponse.id}">${autoResponse.keyword}</a></td>
                    <td>${autoResponse.templatename}</td>
                    <td>${fns:getDictLabel(autoResponse.msgtype,'msg_type','文本消息' )}</td>
                    <td>${autoResponse.addtime}</td>
                    <shiro:hasPermission name="wechat:autoResponse:view"><td>
                        <a href="javascript:;" onclick="update('${autoResponse.id}');">修改</a>
                        <a href="javascript:;" onclick="deleteMsg('确认要删除该文本消息模板吗？','${ctx}/wechat/autoResponse/delete?id=${autoResponse.id}',this);">删除</a>
                    </td></shiro:hasPermission>
                </tr>
            </c:forEach>
            <tr>
                <td colspan="6">
                    <a href="javascript:;" class="btn btn-primary btn-sm" id="addInfo">添加</a>
                </td>
            </tr>
            </tbody>
        </table>
        <div class="pagination">${page}</div>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">添加关键字</h4>
            </div>
            <div class="modal-body">
                <form:form id="inputForm" modelAttribute="autoResponse" action="" method="post" class="form-horizontal">
                    <form:hidden path="id" />
                    <form:hidden path="templatename" />
                    <div class="form-group">
                        <label class="col-sm-2 no-padding-right control-label">关键字:</label>
                        <div class="col-sm-8">
                            <form:input path="keyword" htmlEscape="false" maxlength="20" cssClass="required col-sm-8" placeholder="请输入关键字"/>
                            <span class="important">*</span>
                        </div>
                    </div><hr>
                    <div class="form-group">
                        <label class="col-sm-2 no-padding-right control-label">公众号:</label>
                        <div class="col-sm-8">
                            <form:select path="accountid" cssClass="required col-sm-8">
                                <form:option value="">---请选择---</form:option>
                                <form:options items="${accountList}" itemValue="id" itemLabel="accountName"/>
                            </form:select>
                            <span class="important">*</span>
                        </div>
                    </div><hr>
                    <div class="form-group">
                        <label class="col-sm-2 no-padding-right control-label">消息类型:</label>
                        <div class="col-sm-8">
                            <form:select path="msgtype">
                                <form:option value="">---请选择---</form:option>
                                <form:options items="${fns:getDictList('msg_type')}" itemValue="value" itemLabel="label" />
                            </form:select>
                            <span class="important">*</span>
                        </div>
                    </div><hr>
                    <div class="form-group">
                        <label class="col-sm-2 no-padding-right control-label">消息模板:</label>
                        <div class="col-sm-8">
                            <form:select path="rescontent" cssClass="required col-sm-8">
                                <form:option value="">---请选择---</form:option>
                                <form:options items="${templateList}" itemValue="id" itemLabel="templatename"/>
                            </form:select>
                            <span class="important">*</span>
                        </div>
                    </div>
                </form:form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="btnSubmit">确定</button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function() {
        $("#addInfo").click(function(){
            $("#myModal").modal();
        });

        $("#btnSubmit").click(function(){
            if($("#inputForm").valid()){
                $("#templatename").val($("#rescontent").find("option:selected").text());
                $.post("${ctx}/wechat/autoResponse/save",$("#inputForm").serialize(),function(data){
                    if(data.success){
                        showNote("success", function () {
                            window.location.href = "${ctx}/wechat/autoResponse/list";
                        }, data.msg);
                    }else {
                        showNote("error", function () {
                            return false;
                        }, data.msg);
                    }
                });
            }else{
                showNote("error", function () {
                    return false;
                }, "请输入正确的信息");
            }
        });
    });

    //修改
    function update(id){
        var url = "${ctx}/wechat/autoResponse/find";
        $.post(url,{id:id},function(data){
            $("#id").val(data.id);
            $("#templatename").val(data.templatename);
            $("#keyword").val(data.keyword);
            $("#accountid").find("option[value='"+data.accountid+"']").attr("selected",true);
            $("#msgtype").find("option[value='"+data.msgtype+"']").attr("selected",true);
            $("#rescontent").find("option[value='"+data.rescontent+"']").attr("selected",true);
            $("#myModal").modal();
        });
    }

    //删除
    function deleteMsg(msg,href,tr){
        bootbox.confirm(msg,function(obj){
            if(obj) {
                $.post(href,function(data){
                    if(data.success){
                        showNote("success", function () {
                            $(tr).parent().parent().remove();
                        }, data.msg, "center");
                    }else {
                        showNote("error", function () {
                            return false;
                        }, data.msg);
                    }
                });
            }
        });
    }

    function page(n,s){
        $("#pageNo").val(n);
        $("#pageSize").val(s);
        $("#searchForm").submit();
        return false;
    }
</script>
</body>
</html>
